/*
*实现你自己的使用计数式HasPtr类
*/
#ifndef _DEFINE_
#define _DEFINE_

class U_Ptr;

class HasPtr
{
public:
	//构造函数
	HasPtr(int *p,int i):ptr(new U_Ptr(p)),val(i){}
	//复制构造函数
	HasPtr(const HasPtr& orig):ptr(orig.ptr),val(orig.val){++ptr->use;}
	//赋值操作符
	HasPtr& operator=(const HasPtr& rhs)
	{
		++rhs.ptr->use;
		if(--rhs.ptr->use == 0)
		{
			delete ptr;
		}
		ptr = rhs.ptr;
		val = rhs.val;
		return *this;
	}
	//析构函数
	~HasPtr()
	{
		if(--ptr->use == 0)
		{
			delete ptr;
		}
	}
	//成员函数
	void set_ptr(int *p){ptr->ip = p;}
	void set_int(int i){val = i;}
	int get_ptr_val() const {return *ptr->ip;}
	void set_ptr_val(int i){*ptr->ip = i;}
private:
	int val;
	U_Ptr *ptr;
};

class U_Ptr
{
	friend class HasPtr;
	int *ip;
	size_t use;
	U_Ptr(int *p):ip(p),use(1){}
	~U_Ptr(){delete ip;}
};
#endif



